import numpy

x = numpy.arange(0, 1, 0.01)

def least_squares_polynomial(data, degree):
    """Returns the coefficients to and an evaluator for a least-square
    polynomial interpolation for the given data."""
    sorted = data[data[:, 0].argsort()].T
    return numpy.polyfit(sorted[0], sorted[1], degree)

def polynomial_evaluator(coefficients, x=x):
    """Returns the value at the given point when evaluated by the polynomial
    function defined by the given coefficients."""
    y = 0
    for c in coefficients: y = y*x+c # Horner's method
    return y
